/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_GTK_WIDGET_H
#define GENERIK_GTK_WIDGET_H

/** \file widget.h
This file declares a wrapper class for the GtkWidget class.
*/

#include "init.h"

namespace generik
{
	namespace gtk
	{
	
		/** An encapsulation of GtkWidget.
		It makes the dynamic adding and removing of the widget to/from a GtkContainer easy, and therefore
		serves as a base class to specialise other dynamic GtkWidgets.
		*/
		struct widget
		{
			GtkWidget* _widget;
			GtkContainer* _container;
		
			/** Constructor which accepts a widget (if specified) and automatically adds it to the specified
			container.
			*/
			widget(GtkWidget* widget_in = 0, GtkContainer* container_in = 0)
			: _widget(0)
			, _container(0)
			{
				GENERIK_TRACER;
				set_widget(widget_in);
				add_to(container_in);
			}
		
			/** Destructor which automatically removes the held widget from its container and then decreases
			the reference count to it (See remove() and set_widget()).
			*/
			~widget()
			{
				GENERIK_TRACER;
				remove();
				set_widget(0);
			}
		
			/** Replaces the widget with the specified one.
			This method tries to make sure the old widget is destroyed before accepting the new one,
			by calling g_object_unref() on it. 
			\note If the old widget should <i>not</i> be destroyed, another
			reference <i>has</i> to be added (using g_object_ref) right before calling this!
			\note Also, note that after the specified widget is accepted, this instance
			adds a reference to it.
			*/
			void set_widget(GtkWidget* widget_in)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				if (_widget)
				{
					g_object_unref(G_OBJECT(_widget));
					//gtk_widget_destroy(_widget);
				}
				_widget = widget_in;
				if (_widget) g_object_ref(G_OBJECT(_widget));
			}
		
			/** Adds this widget to a container widget.
			*/
			void add_to(GtkContainer* container_in)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				_container = container_in;
				if (_container)
				{
					GENERIK_TRACE("adding widget ["<<_widget<<"] to container ["<<_container<<"]");
					gtk_container_add(GTK_CONTAINER(_container), _widget);
				}
			}
		
			/** Removes this widget from the container it is stored in.
			\note Because this instance holds a reference to its widget, removing it does <i>not</i> destroy it.
			\note added a GTK_IS_CONTAINER check. If the (for example) main window is destroyed in a delete_event,
			and a widget instance persists beyond this event, it won't get notified, and will try to remove itself from
			a non-existent container upon destruction. This check prevents that.
			*/
			void remove()
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				if (_container && GTK_IS_CONTAINER(_container))
				{
					GENERIK_TRACE("removing widget ["<<_widget<<"] from container ["<<_container<<"]");
					gtk_container_remove(GTK_CONTAINER(_container), _widget);
				}
				_container = 0;
			}
			
			std::string get_name()
			{
				return gtk_widget_get_name(get_widget());
			}
			
			/** Returns the widget encapsulated by this instance.
			*/
			virtual GtkWidget* get_widget()
			{
				return _widget;
			}
			
			virtual GObject* get_object()
			{
				return G_OBJECT(_widget);
			}
			
			virtual GdkWindow* get_parentwindow()
			{
				return gtk_widget_get_parent_window(_widget);
			}
			
			GtkContainer* get_container()
			{
				GENERIK_TRACER;
				GENERIK_TRACE(_container);
				return _container;
			}
		
			/** Sets the visibility of this widget.
			*/
			virtual void set_visible(bool value)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				g_object_set(_widget, "visible", value, (char*)0);
			}
		
			/** Gets the visibility of this widget.
			*/
			virtual gboolean get_visible()
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				gboolean value;
				g_object_get(_widget, "visible", &value, (char*)0);
				return value;
			}
		};

	}
}

#endif


